package com.linkstec.jexl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ThreadJexlTest implements Runnable{
	String ex = "(a > 1 & true ) | b <= 4 || c == 10";
	
	static JexlContext jc = new MapContext();
	long start = System.currentTimeMillis();
	static boolean bbb = false;
	private static final Logger logger = LogManager.getLogger();

	public static void main(String[] args) {
		jc.set("a", 10);
		jc.set("b", 1);
		jc.set("c", 10);
		long start = System.currentTimeMillis();
		ExecutorService es = Executors.newFixedThreadPool(8);
		for (int i = 0; i < 8; i++){
			es.submit(new ThreadJexlTest());
		}
		es.shutdown();
		try {
			es.awaitTermination(100, TimeUnit.SECONDS);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		logger.info((end - start));
		if (bbb) {
			logger.info("true");
		} else {
			logger.info("false");
		}
		String ex2 = "c == 10";
		JexlContext jc2 = new MapContext();
		jc2.set("c", 10);
		boolean boo2 = parseBoolean(ex2, jc2);
		logger.info(boo2);

	}
	public static boolean parseBoolean(String ex, JexlContext jc) {
		Expression e = new JexlEngine().createExpression(ex);
		Object result = e.evaluate(jc);
		if (result instanceof Boolean) {
			return (boolean) result;
		}
		long i = (long) result;
		if (i ==1) {
			return true;
		} else {
			return false;
		}
	}
	@Override
	public void run() {
//		int loop = 100000;
		int loop = 10;
		for (int i = 0; i < loop; i++){
			bbb = parseBoolean(ex, jc);
		}
		
	}
}